#!/usr/bin/perl

# Sweeps name
$name = "sim";

# Sweeps replicates
$replicates_per_sweep = 3;

# Neutral microsatellite mut rate is 10^-4, 10^-5, 10^-7 (literature based)
$neutral_mutation_rate[0] = 1/10000;
$neutral_mutation_rate[1] = 1/100000;
$neutral_mutation_rate[2] = 1/10000000;

# Mutation rate of the selective loci
# We have 10^-5 .. 10^-8
# 9 loci total, 3 survs, 3 repros, 1 mutators
$selective_mutation_rate[0] = 1/100000;
$selective_mutation_rate[1] = 1/1000000;
$selective_mutation_rate[2] = 1/10000000;
$selective_mutation_rate[3] = 1/100000000;


# Sweep selective coefficient
$selective_coefficient[0] = 0.001;
$selective_coefficient[1] = 0.01;
$selective_coefficient[2] = 0.1;
$selective_coefficient[3] = 0.5;
$selective_coefficient[4] = 1.0;
$selective_coefficient[5] = 2.0;

# division prob
$crypt_div_prob[0] = 0.0;
$crypt_div_prob[1] = 0.25;
$crypt_div_prob[2] = 0.5;
$crypt_div_prob[3] = 0.75;
$crypt_div_prob[4] = 1.0;

# multidim
my $data;
my $fname = 0;
# Sweep div prob
for $divprob (0..4) {
    # Sweep selective coefficient
    for $selcoeff (0..5){
	# Sweep selective mut rate
	for $selmu (0..3){
	    # Sweep neutral mut rate
	    for $neutmu (0..2){
		# Number of repro and surv hits
		for $selloci (5..5) {
		    # Repeat replicates times
		    for $replicates (0..$replicates_per_sweep-1){
			# total sweep count
			if(!defined($data[$selmu][$divprob][$selcoeff][0])){
			    $data[$selmu][$divprob][$selcoeff][0]=0;
			}
			$data[$selmu][$divprob][$selcoeff][0]++;
			# running count
			if(!defined($data[$selmu][$divprob][$selcoeff][1])){
			    $data[$selmu][$divprob][$selcoeff][1]=0;
			}
			#$fname = ($divprob+1)*($selcoeff+1)*($selmu+1)*($neutmu+1)*($replicates+1);
			$fname++;
			$nn = "../$name$fname/$name$fname.fixations.log";
			print $nn."\n";
			open(IN, "../$name$fname/$name$fname.fixations.log");
			$reproductivefixations=0;
			while(<IN>){
			    chomp;
			    @row = split(/\,/);
			    if($row[3]>0){
				$reproductivefixations++;
				#print STDERR $reproductivefixations."\n";
			    }
			    #print STDERR $row[3]."\n";
			}
			$data[$selmu][$divprob][$selcoeff][1]+=$reproductivefixations;
			close IN;			
		    }
		}
	    }
	}	
    }
}

for $selmu (0..3){
    for $selcoeff (0..5){
	for $divprob (0..4) {
	    if($divprob<4){
		print sprintf("%0.2f", $data[$selmu][$divprob][$selcoeff][1]/$data[$selmu][$divprob][$selcoeff][0]).",";
	    } else {
		print sprintf("%0.2f", $data[$selmu][$divprob][$selcoeff][1]/$data[$selmu][$divprob][$selcoeff][0]);
	    }
	}
	print "\n";
    }
}


print "\n";
print $data[$selmu][$divprob][$selcoeff][0]."\n";
print "\n";

for $selmu (0..3){
    for $selcoeff (0..5){
	for $divprob (0..4) {
	    if($divprob<4){
		print $data[$selmu][$divprob][$selcoeff][1].",";
	    } else {
		print $data[$selmu][$divprob][$selcoeff][1];
	    }
	}
	print "\n";
    }
}

